// pages/shopDetail/shopDetail.js
let http = require('../../utils/request.js')
let path = getApp().globalData.path
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '商城',
    background: [],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    baseurl: path,
    moment: [],
    show: false,
    show2: false
  },
  // 打开规格面板
  goAddShopCartPopup(e) {
    let _this = this
    if (_this.data.tmp.labels.length !== 0) {
      this.setData({
        show: true,
      })
    } else if (_this.data.tmp.labels.length == 0) {
      let goodsid = _this.data.goodsid

      if (wx.getStorageSync('userid')) {

        // 添加购物车
        http.postHttp('/user_cart_add', {
          goods_num: 1,
          goods_id: goodsid,
          unit: ''
        }, {
          token: wx.getStorageSync('token')
        }).then(res => {
          if (res.data.code == 200) {
            console.log('添加购物车', res)
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
          wx.navigateTo({
            url: '/pages/shopCart/shopCart',
          })
        })
      } else {
        wx.showToast({
          title: '请先登录',
          icon: 'none'
        })
        wx.redirectTo({
          url: '/pages/userLogin/userLogin',
        })
      }
    }
  },
  // 打开规格面板2
  goAddShopCartPopup2(e) {
    let _this = this
    if (_this.data.tmp.labels.length !== 0) {
      this.setData({
        show2: true,
      })
    } else if (_this.data.tmp.labels.length == 0) {
      let goodsid = _this.data.goodsid
      let integral = _this.data.tmp.integral

      if (wx.getStorageSync('userid')) {

        if (integral == 0) {
          wx.setStorageSync('buyType', 1) //商城直接购买
          wx.navigateTo({
            url: `/pages/confirmOrder/confirmOrder?goodsid=${goodsid}&price=${_this.data.price}&unit=''&isIntegral=0`,
          })
        }else{
          wx.setStorageSync('buyType', 1) //商城直接购买
          wx.navigateTo({
            url: `/pages/confirmOrder/confirmOrder?goodsid=${goodsid}&price=${_this.data.price}&unit=''&isIntegral=1`,
          })
        }
      } else {
        wx.showToast({
          title: '请先登录',
          icon: 'none'
        })
        wx.redirectTo({
          url: '/pages/userLogin/userLogin',
        })
      }
    }
  },
  // 确认添加按钮
  addShopCart(e) {
    let _this = this
    let unit = e.currentTarget.dataset.unit
    let goodsid = _this.data.goodsid
    console.log(unit)

    if (wx.getStorageSync('userid')) {

      // 添加购物车
      if (unit) {
        http.postHttp('/user_cart_add', {
          goods_num: 1,
          goods_id: goodsid,
          unit: unit
        }, {
          token: wx.getStorageSync('token')
        }).then(res => {
          wx.showToast({
            title: '选择成功',
            icon: 'none'
          })
          if (res.data.code == 200) {
            this.setData({
              show: false,
            })
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
          wx.navigateTo({
            url: '/pages/shopCart/shopCart',
          })
          this.setData({
            show:false
          })
        })
      } else {
        wx.showToast({
          title: '请先选择商品参数',
          icon: 'none'
        })
      }
    } else {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      wx.redirectTo({
        url: '/pages/userLogin/userLogin',
      })
    }

  },
  // 跳转规格页面
  goUnitParam(e) {
    let _this = this
    wx.navigateTo({
      url: `/pages/unitParam/unitParam?goodsid=${_this.data.goodsid}`,
    })
  },
  // 立即购买
  buyNow(e) {
    let _this = this
    let goodsid = _this.data.goodsid
    let unit = e.currentTarget.dataset.unit
    let integral = _this.data.tmp.integral

    if (wx.getStorageSync('userid')) {

      if (unit) {
        wx.showToast({
          title: '选择成功',
          icon: 'none'
        })
        if (integral == 0) {
          wx.setStorageSync('buyType', 1) //商城直接购买
          wx.navigateTo({
            url: `/pages/confirmOrder/confirmOrder?goodsid=${goodsid}&price=${_this.data.price}&unit=${unit}&isIntegral=0`,
          })
          this.setData({
            show2:false
          })
        }


      } else {
        wx.showToast({
          title: '请先选择商品参数',
          icon: 'none'
        })
      }
    } else {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      wx.redirectTo({
        url: '/pages/userLogin/userLogin',
      })
    }
  },


  // Swiper切换
  onShareAppMessage() {
    return {
      title: 'swiper',
      path: 'page/component/pages/swiper/swiper'
    }
  },
  // 联系客服
  goService(e) {
    wx.makePhoneCall({
      phoneNumber: '18011524442',
      success() {
        console.log('拨打成功')
      },
      fail() {
        console.log('拨打失败')
      }
    })
  },
  // 加入购物车
  joinShopCart(e) {
    let _this = this
    let goodsid = _this.data.goodsid
    let unit = _this.data.unit
    if (unit) {

      // 添加购物车
      if (wx.getStorageSync('userid')) {
        http.postHttp('/user_cart_add', {
          goods_num: 1,
          goods_id: goodsid,
          unit: unit
        }, {
          token: wx.getStorageSync('token')
        }).then(res => {
          if (res.data.code == 200) {
            console.log('添加购物车', res)
            this.setData({
              show: false,
            })
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
          // wx.navigateTo({
          //   url: '/pages/shopCart/shopCart',
          // })
        })
      } else {
        wx.redirectTo({
          url: '/pages/userLogin/userLogin',
        })
      }
    } else {
      wx.showToast({
        title: '请先选择商品参数',
        icon: 'none'
      })
    }
  },
  // 跳转购物车
  goShopCart(e) {
    wx.navigateTo({
      url: '/pages/shopCart/shopCart',
    })
  },
  // popup关闭
  onClose() {
    this.setData({
      show: false
    });
  },
  onClose2() {
    this.setData({
      show2: false
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this
    let price = options.price
    let goodsid = options.goodsid
    this.setData({
      price,
      goodsid
    })
    // 商品列表
    http.getHttp('/shop_goods_detail', {
      goods_id: goodsid
    }, {}).then(res => {
      console.log('detailRes', res)
      let tmpArr = res.data.data
      let tmpPic = res.data.data.goods_images
      let valImg = ''
      let img = tmpPic.map((val, index) => {
        valImg = val.goods_images
        return val;
      })
      // console.log(res.data.data.goods_image,'12')
      this.setData({
        background: tmpPic,
        tmp: res.data.data,
        tmpArr,
        goodsid,
        img,
        ['tmp.goods_image']: this.data.baseurl + res.data.data.goods_image
      })
    })
    // 评论列表
    http.getHttp('/goods_comment_list', {
      goods_id: _this.data.goodsid,
      page: 1,
      limit: 2
    }, {}).then(res => {
      console.log('评论详情', res)
      let commentArr = res.data.data
      this.setData({
        commentTmp: commentArr
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {


  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})